//
//  ViewController.m
//  10-HomeWork
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Frame.h"
#import "CarNewsTableViewCell.h"
#import "ButtonAndLabel.h"
#import "CarNews.h"
#import "SelfSizeTool.h"
#define kTableViewCellReuse @"reuse"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic, strong)UITableView *carTableView;
@property (nonatomic, strong)UIView *fourTypeView;
@property (nonatomic, strong)UIView *headView;
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)NSMutableArray *carListArray;
@property (nonatomic, strong)NSMutableArray *imageNameArr;
@property (nonatomic, strong)UIPageControl *pageControll;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _carListArray = [NSMutableArray array];
     [self loadData];
    
    // Do any additional setup after loading the view, typically from a nib.
    _carTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_carTableView];
    _carTableView.delegate = self;
    _carTableView.dataSource = self;
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.pf_width, 300)];
    _carTableView.tableHeaderView = _headView;
    [self loadScrollView];
    [self createPageControll];
    [self loadFourNum];
    [_carTableView registerClass:[CarNewsTableViewCell class] forCellReuseIdentifier:kTableViewCellReuse];
}
#pragma mark - delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    CarNews *carNews = _carListArray[indexPath.row];
//    NSString *string = carNews.title;
//    
//    
//    return [SelfSizeTool selfSizeWithString:string width:self.view.pf_width * 2 / 3 font:[UIFont systemFontOfSize:17] ];
    return 100;
}
#pragma mark - dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _carListArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CarNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    cell.carNews = _carListArray[indexPath.row];
    return cell;
}

#pragma mark - fourNum
- (void)loadFourNum {
    _fourTypeView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.pf_width, 100)];
    
    
    ButtonAndLabel *buttonAndLable1 = [[ButtonAndLabel alloc] initWithFrame:CGRectMake(5, 5, 90, 100) button:[UIImage imageNamed:@"1"] label:@"贷款买车"];
    [_fourTypeView addSubview:buttonAndLable1];
    
    ButtonAndLabel *buttonAndLable2 = [[ButtonAndLabel alloc] initWithFrame:CGRectMake(_fourTypeView.pf_width * 1 / 4, 5, 90, 100) button:[UIImage imageNamed:@"2"] label:@"直销车型"];
    [_fourTypeView addSubview:buttonAndLable2];
    
    ButtonAndLabel *buttonAndLable3 = [[ButtonAndLabel alloc] initWithFrame:CGRectMake(_fourTypeView.pf_width * 2 / 4, 5, 90, 100) button:[UIImage imageNamed:@"3"] label:@"底价买车"];
    [_fourTypeView addSubview:buttonAndLable3];
    
    ButtonAndLabel *buttonAndLable4 = [[ButtonAndLabel alloc] initWithFrame:CGRectMake(_fourTypeView.pf_width * 3 / 4, 5, 90, 100) button:[UIImage imageNamed:@"4"] label:@"二手车"];
    [_fourTypeView addSubview:buttonAndLable4];
    [_headView addSubview:_fourTypeView];
    NSLog(@"%f", self.view.pf_width);
    NSLog(@"%f", self.view.pf_height);
}
#pragma mark - pageControll 
- (void) createPageControll {
    _pageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _scrollView.pf_height - 50, self.view.pf_width, 50)];
    _pageControll.numberOfPages = _imageNameArr.count - 2;
    _pageControll.currentPage = 0;
    [_headView addSubview:_pageControll];
    [_pageControll addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
}
- (void)valueChange:(UIPageControl *)pageControl {
    NSInteger i = pageControl.currentPage;
    _scrollView.contentOffset = CGPointMake((i + 1) * self.view.pf_width, 0);
}
#pragma mark - loadScrollView
- (void)loadScrollView {
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.pf_width, 200)];
    _scrollView.contentSize = CGSizeMake(self.view.pf_width * _imageNameArr.count, 200);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentOffset = CGPointMake(self.view.pf_width, 0);
    for (int i = 0; i < _imageNameArr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.pf_width, 0, self.view.pf_width, 200)];
        imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_imageNameArr[i]]]];
        
        [_scrollView addSubview:imageView];
    }
    [_headView addSubview:_scrollView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == _scrollView) {
        NSInteger i = scrollView.contentOffset.x / self.view.pf_width;
    if (i == 0) {
        scrollView.contentOffset = CGPointMake(self.view.pf_width * (_imageNameArr.count - 2), 0);
        _pageControll.currentPage = _imageNameArr.count - 1 - 2;
    } else if (i == _imageNameArr.count - 1) {
        scrollView.contentOffset = CGPointMake(self.view.pf_width, 0);
        _pageControll.currentPage = 0;
    } else {
        _pageControll.currentPage = i - 1;
    }
    }
    
}

#pragma mark - loadData
- (void)loadData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
    NSDictionary *sourceDic = [NSDictionary dictionaryWithContentsOfFile:path];
   //获取data字典
    NSDictionary *dataDic = [sourceDic objectForKey:@"data"];
   //获取cycleImage数组
    NSArray *cycleImageArray = [dataDic objectForKey:@"cycleImage"];
    _imageNameArr = [NSMutableArray array];
    for (NSDictionary *dic in cycleImageArray) {
        [_imageNameArr addObject:[dic objectForKey:@"picCover"]];
    }
    [_imageNameArr removeLastObject];
    NSString *firstImageName = [[_imageNameArr firstObject] copy];
    NSString *lastImageName = [[_imageNameArr lastObject] copy];
    [_imageNameArr insertObject:lastImageName atIndex:0];
    [_imageNameArr insertObject:firstImageName atIndex:_imageNameArr.count ];
   
    
   //获取list数组
    
    NSArray *array = [dataDic objectForKey:@"list"];
    
    for (NSDictionary *dic in array) {
        CarNews *carNews = [[CarNews alloc] initWithImageName:[dic objectForKey:@"picCover"] title:[dic objectForKey:@"title"]];
        [_carListArray addObject:carNews];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
